function WebGLGaugeShader()
{
	this.shaderProgram = null;	
	
	this.vertexPositionAttribute = null;		
	
	//buffer	
	this.verticesBuffer = null;
	this.stickVerticesBuffer = null;			
}
	
//
// initShaders
//
// Initialize the shaders, so WebGL knows how to light our scene.
//
WebGLGaugeShader.prototype.initShaders = function(gl, vsName, fsName) 
{
  var fragmentShader = this.getShader(gl, vsName);
  var vertexShader = this.getShader(gl, fsName);
  
  // Create the shader program
  
  this.shaderProgram = gl.createProgram();
  gl.attachShader(this.shaderProgram, vertexShader);
  gl.attachShader(this.shaderProgram, fragmentShader);
  gl.linkProgram(this.shaderProgram);
  
  // If creating the shader program failed, alert
  
  if (!gl.getProgramParameter(this.shaderProgram, gl.LINK_STATUS)) 
  {
	alert("Unable to initialize the shader program.");
  }
  
  gl.useProgram(this.shaderProgram);
  
  this.vertexPositionAttribute = gl.getAttribLocation(this.shaderProgram, "aVertexPosition");
  gl.enableVertexAttribArray(this.vertexPositionAttribute);
  
 // this.vertexColorAttribute = gl.getAttribLocation(this.shaderProgram, "aVertexColor");
 // gl.enableVertexAttribArray(this.vertexColorAttribute); 
  
  if(this.vertexPositionAttribute === null)
  {
	  alert("vertexPositionAttribute is null");
  }
}

WebGLGaugeShader.prototype.initGaugeBuffers = function(gl)
{  
  
	//prepare data
	var vertices = new Array();
	var PI = 3.14159;
	var vertexNum = 72;
	var edgeAngle = 2.0*PI/vertexNum;
	var radius = 0.1;	
	var rd1 = radius;
	var rd2 = radius * 0.85;
	var u = $V([1.0, 0.0, 0.0]);
	var v = $V([0.0, 1.0, 0.0]);
	
	for(var i = 0; i<vertexNum+2; i++)
	{
	//	var t1 = rd1 * (Math.cos(theta) * u + Math.sin(theta) * v);
	//	var t2 = rd2 * (Math.cos(theta) * u + Math.sin(theta) * v);
	
		var theta = (i%vertexNum) * edgeAngle;
		var k1 = u.x(Math.cos(theta));
		var k2 = v.x(Math.sin(theta));		
		var k3 = k1.add(k2);
		var t1 = k3.x(rd1);
		var t2 = k3.x(rd2);		
		
		vertices[2*i*3]     = t1.e(1);
		vertices[2*i*3 + 1] = t1.e(2);
		vertices[2*i*3 + 2] = t1.e(3);					
		
		var j = 2*i+1;			
		vertices[j*3]     = t2.e(1);
		vertices[j*3 + 1] = t2.e(2);
		vertices[j*3 + 2] = t2.e(3);	
	}
	
	// Create a buffer for the cube's vertices.
  
	this.verticesBuffer = gl.createBuffer();
  
	// Select the cubeVerticesBuffer as the one to apply vertex
	// operations to from here out.
	gl.bindBuffer(gl.ARRAY_BUFFER, this.verticesBuffer);		
	gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
	
	//prepare stick data	
	var smallRadiusRatio = 0.08;
	var stickLength = 1.4;
	var pos1 = $V([0.0, 0.0, 0.0]);
	var pos2 = (($V([0.0, 0.0, 1.0])).x(radius * stickLength)).add(pos1);
	var rSmall = radius * smallRadiusRatio;
	for(var i = 0; i<vertexNum+2; i++)
	{	
		var theta = (i%vertexNum) * edgeAngle;
		var k1 = u.x(Math.cos(theta));
		var k2 = v.x(Math.sin(theta));		
		var k3 = (k1.add(k2)).x(rSmall);
		var t1 = k3.add(pos1);
		var t2 = k3.add(pos2);		
		
		vertices[2*i*3]     = t1.e(1);
		vertices[2*i*3 + 1] = t1.e(2);
		vertices[2*i*3 + 2] = t1.e(3);					
		
		var j = 2*i+1;			
		vertices[j*3]     = t2.e(1);
		vertices[j*3 + 1] = t2.e(2);
		vertices[j*3 + 2] = t2.e(3);	
	}
	this.stickVerticesBuffer = gl.createBuffer();
	gl.bindBuffer(gl.ARRAY_BUFFER, this.stickVerticesBuffer);		
	gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);	
}


//
// getShader
//
// Loads a shader program by scouring the current document,
// looking for a script with the specified ID.
//
WebGLGaugeShader.prototype.getShader = function(gl, id) 
{
  var shaderScript = document.getElementById(id);
  
  // Didn't find an element with the specified ID; abort.
  
  if (!shaderScript) 
  {
	return null;
  }
  
  // Walk through the source element's children, building the
  // shader source string.
  
  var theSource = "";
  var currentChild = shaderScript.firstChild;
  
  while(currentChild) 
  {
	if (currentChild.nodeType == 3)
	{
	  theSource += currentChild.textContent;
	}
	
	currentChild = currentChild.nextSibling;
  }
  
  // Now figure out what type of shader script we have,
  // based on its MIME type.
  
  var shader;
  
  if (shaderScript.type == "x-shader/x-fragment") 
  {
	shader = gl.createShader(gl.FRAGMENT_SHADER);
  } 
  else if (shaderScript.type == "x-shader/x-vertex")
  {
	shader = gl.createShader(gl.VERTEX_SHADER);
  } 
  else 
  {
	return null;  // Unknown shader type
  }
  
  // Send the source to the shader object
  
  gl.shaderSource(shader, theSource);
  
  // Compile the shader program
  
  gl.compileShader(shader);
  
  // See if it compiled successfully
  
  if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS))
  {
	alert("An error occurred compiling the shaders: " + gl.getShaderInfoLog(shader));
	return null;
  }
  
  return shader;
}	



